<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="multipart/form-data; charset=utf-8" />
    <title>Title</title>

</head>
<body>
<!--<form id="uploadForm" method="post" enctype="multipart/form-data">
    <input type="file" id="fileName" name="file">
</form>
<input type="button" id="btn" value="提交">-->

文件:<input id="file" type="file" name="file"/>
<button id="upload">上传文件</button>

</body>
<script src="js/jquery.min.js" th:href="@{js/jquery.min.js}"></script>
<script>
 /*  $("#btn").click(function () {
       var formData = new FormData($('#uploadForm')[0]);
       $.ajax({
           type: 'post',
           url: "/upload",
           data:formData,
           cache: false,
           processData: false,
           contentType: false,
       }).success(function (data) {
           alert(data);
       }).error(function () {
           alert("上传失败");
       });
   });*/

 $(function () {
     $("#upload").click(function(){
         var formData = new FormData();
         formData.append('file', $('#file')[0].files[0]);
         console.log(formData);
         $.ajax({
             url: "/upload",
             type: "POST",
             data: formData,
             /**
              *必须false才会自动加上正确的Content-Type
              */
             contentType: false,
             /**
              * 必须false才会避开jQuery对 formdata 的默认处理
              * XMLHttpRequest会对 formdata 进行正确的处理
              */
             processData: false,
             success: function (data) {
                 alert(data);

             },
             error: function () {
                 alert("上传失败！");

             }
         });
     })
 });
</script>
</html>